//选择排序：不断的选择后面最小的和当前元素进行交换
//时间复杂度：O(n^2)

function selectionSort(arr) {
  let n = arr.length;
  for (let i = 0; i < n; i++) {
    let minNum = arr[i];
    let minIndex = i;
    for (let j = i; j < n; j++) {
      if (minNum > arr[j]) {
        minNum = arr[j];
        minIndex = j;
      }
    }
    [arr[minIndex], arr[i]] = [arr[i], arr[minIndex]];
  }
  return arr;
}
const arr = [5, 4, 3, 2, 1];
console.log(selectionSort(arr));
